from django.shortcuts import render, redirect, HttpResponse
from employee.models import employee, department, group, employeeinfo
from employee.forms import TestForm


# 操作部门数据表函数
def list_dep_old(request):
    dep_list = department.objects.all()
    return render(request, 'test_orm_old/list_dep_old.html', {'dep_list': dep_list})


def add_dep_old(request):
    if request.method == 'POST':
        dep_name = request.POST.get('dep_name')
        dep_script = request.POST.get('dep_script')
        if not dep_name:
            return render(request, 'test_orm_old/add_dep_old.html', {'error_info': "部门名字不能为空"})
        try:
            department.objects.create(dep_name=dep_name, dep_script=dep_script)
            return redirect('/test_orm_old/list_dep_old/')
        except Exception as e:
            return render(request, 'test_orm_old/add_dep_old.html', {'error_info': "输入部门名称重复或错误"})
        finally:
            pass
    return render(request, 'test_orm_old/add_dep_old.html')


def del_dep_old(request, dep_ip):
    department.objects.filter(id=dep_ip).delete()
    return redirect('/test_orm_old/list_dep_old/')


def edit_dep_old(request, dep_id):
    if request.method == 'POST':
        id = request.POST.get('id')
        dep_name = request.POST.get('dep_name')
        dep_script = request.POST.get('dep_script')

        edit_obj = department.objects.get(id=id)
        edit_obj.dep_name = dep_name
        edit_obj.dep_script = dep_script
        edit_obj.save()
        return redirect('/test_orm_old/list_dep_old/')
    else:
        dep_obj = department.objects.get(id=dep_id)
        return render(request, 'test_orm_old/edit_dep_old.html', {'department': dep_obj})


# 操作员工数据表函数
def list_employee_old(request):
    emp_list = employee.objects.all()
    return render(request, 'test_orm/list_employee.html', {'emp_list': emp_list})


def add_employee_old(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        email = request.POST.get('email')
        salary = request.POST.get('salary')
        dep = request.POST.get('dep')
        info = request.POST.get('info')
        group_ = request.POST.getlist('group')

        new_emp = employee.objects.create(name=name, email=email, salary=salary, dep_id=dep, info_id=info)
        new_emp.group.set(group_)
        return redirect('/test_orm_old/list_employee_old/')

    else:
        dep_list = department.objects.all()
        group_list = group.objects.all()
        info_list = employeeinfo.objects.all()
        return render(request, 'test_orm/add_employee.html', {'info_list': info_list, 'group_list': group_list,
                                                                      'dep_list': dep_list})


def del_employee_old(request, emp_id):
    employee.objects.filter(id=emp_id).delete()
    return redirect('/test_orm_old/list_employee_old/')


def edit_employee_old(request, emp_id):
    if request.method == 'POST':
        id = request.POST.get('id')
        name = request.POST.get('name')
        salary = request.POST.get('salary')
        email = request.POST.get('email')
        dep = request.POST.get('dep')
        info = request.POST.get('info')
        groups = request.POST.getlist('group')

        emp_obj = employee.objects.get(id=id)
        emp_obj.name = name
        emp_obj.salary = salary
        emp_obj.email =email
        emp_obj.dep_id =dep
        emp_obj.info_id = info
        emp_obj.group.set(groups)
        emp_obj.save()

        return redirect('/test_orm_old/list_employee_old/')
    else:
        employee_ = employee.objects.get(id=emp_id)
        dep_list = department.objects.all()
        group_list = group.objects.all()
        info_list = employeeinfo.objects.all()
        return render(request, 'test_orm_old/edit_employee_old.html', {'info_list': info_list, 'group_list': group_list,
                                                                      'dep_list': dep_list, "employee": employee_})


# 操作团队数据表函数
def list_group_old(request):
    group_list = group.objects.all()
    return render(request, 'test_orm_old/list_group_old.html', {'group_list': group_list})


def add_group_old(request):
    if request.method == 'POST':
        group_name = request.POST.get('group_name')
        group_script = request.POST.get('group_script')
        if not group_name:
            return render(request, 'test_orm_old/add_group_old.html', {'error_info': "必须传入团队名称"})
        try:
            group.objects.create(group_name=group_name, group_script=group_script)
            return redirect('/test_orm_old/list_group_old/')
        except Exception as e:
            return render(request, 'test_orm_old/add_group_old.html', {'error_info': "输入团队名称重复或错误"})
        finally:
            pass

    return render(request, 'test_orm_old/add_group_old.html')


def del_group_old(request, group_id):
    group.objects.filter(id=group_id).delete()
    return redirect('/test_orm_old/list_group_old/')


def edit_group_old(request, group_id):
    if request.method == 'POST':
        id = request.POST.get('id')
        group_name = request.POST.get('group_name')
        group_script = request.POST.get('group_script')
        group_obj = group.objects.get(id=id)
        group_obj.group_name = group_name
        group_obj.group_script = group_script
        group_obj.save()
        return redirect('/test_orm_old/list_group_old/')
    else:
        group_obj = group.objects.get(id=group_id)
        return render(request, 'test_orm_old/edit_group_old.html', {'group': group_obj})
    pass


# 操作员工补充数据表函数
def list_employeeinfo_old(request):
    employeeinfo_list = employeeinfo.objects.all()
    return render(request, 'test_orm_old/list_employeeinfo_old.html', {'employeeinfo_list': employeeinfo_list})


def add_employeeinfo_old(request):
    if request.method == 'POST':
        info_phone = request.POST.get("info_phone")
        info_address = request.POST.get('info_address')
        if not all([info_phone, info_address]):
            return render(request, 'test_orm_old/add_employeeinfo_old.html', {'error_info': '请完善手机号码和地址'})
        try:
            employeeinfo.objects.create(phone=info_phone, address=info_address)
            return redirect('/test_orm_old/list_employeeinfo_old/')
        except Exception as e:
            return redirect(request, 'test_orm_old/add_employeeinfo_old.html', {'error_info': '手机号码或地址错误'})
        finally:
            pass
    return render(request, 'test_orm_old/add_employeeinfo_old.html')


def del_employeeinfo_old(request, info_id):
    employeeinfo.objects.filter(id=info_id).delete()
    return redirect('/test_orm_old/list_employeeinfo_old/')


def edit_employeeinfo_old(request, info_id):
    if request.method == 'POST':
        id = request.POST.get('id')
        phone = request.POST.get('phone')
        address = request.POST.get('address')

        employeeinfo_obj = employeeinfo.objects.get(id=id)
        employeeinfo_obj.phone = phone
        employeeinfo_obj.address = address
        employeeinfo_obj.save()

        return redirect('/test_orm_old/list_employeeinfo_old')

    else:
        employeeinfo_obj = employeeinfo.objects.get(id=info_id)
        return render(request, 'test_orm_old/edit_employeeinfo_old.html', {'employeeinfo': employeeinfo_obj})


def index(request):
    return render(request, 'test_orm/index.html')


def test_form(request):
    if request.method == 'POST':
        pass

    else:
        test_form = TestForm()
        return render(request, 'test_form/test_form.html', {'testform': test_form})
